package com.epam.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.epam.dao.DAOFactory;
import com.epam.dao.DAOFactory.DAOType;
import com.epam.dao.intf.ITouristVoucherDAO;
import com.epam.entity.Hotel;
import com.epam.entity.TouristVoucher;
import com.epam.entity.TouristVoucher.Country;
import com.epam.entity.TouristVoucher.Transport;
import com.epam.entity.TouristVoucher.Type;
import com.epam.entity.User;

public class TourTagHandler extends TagSupport {

	private static final long serialVersionUID = 1L;
	private static Logger LOG = Logger.getLogger(TourTagHandler.class);

	private TouristVoucher voucher;

	public int doStartTag() {
			
		ITouristVoucherDAO tvd = (ITouristVoucherDAO) DAOFactory.INSTANCE.getDAO(DAOType.TOURISTVOUCHER);

			int id = voucher.getId();
			Country country = voucher.getCountry();
			int duration = voucher.getDuration();
			Type type = voucher.getType();
			Transport transport = voucher.getTransport();
			Hotel hotel = voucher.getHotel();
			int cost = voucher.getCost();
			boolean lastMinute = voucher.isLastMinute();
			boolean available = tvd.isAvailable(voucher.getId());
			
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			String uri = request.getRequestURI();
			String pageName = uri.substring(uri.lastIndexOf("/")+1);
			
			JspWriter out = pageContext.getOut();
			
			HttpSession session = request.getSession();

			try {
				out.println("<h1 style='border: 1px black solid; font-size: 15px;'>");
				out.println("<tr>");
				
				if (session.getAttribute("role").equals(User.Role.ADMIN)) {
					out.println("<td>" + id + "</td>");
					out.println("<td>" + available + "</td>");
				}
				out.println("<td>" + country + "</td>");
				out.println("<td> " + duration + " days </td>");
				out.println("<td>" + type + "</td>");
				out.println("<td>" + transport + "</td>");
				out.println("<td>" + hotel.getName() + "</td>");
				out.println("<td>" + cost + "$ </td>");
				if (lastMinute) {
					out.println("<font color='red'><td>HOT DEAL!</font>");
				}
				out.println("</tr>");
				
				if (session.getAttribute("role").equals(User.Role.USER)) {
				out.println("<form action='controller' name='buyForm' method='post'>"
							+ "<input type='hidden' name='voucher' value=" + voucher.getId() +">" 
							+ "<input type='hidden' name='action' value='buy'>"
							+ "<input type='submit' value='Buy'></form>");
				
				if (!pageName.equals("shoppingCart.jsp")) {
				
					out.println("<form action='controller' name='addToCart' method='post'>"
							+ "<input type='hidden' name='action' value='addToCart'>"
							+ "<input type='hidden' name='voucher' value=" + voucher.getId() + ">"
							+ "<input type='submit' value='Add to cart'></form>");
					
				}
				
				if (pageName.equals("shoppingCart.jsp")) {
					
					out.println("<form action='controller' name='removeFromCart' method='post'>"
							+ "<input type='hidden' name='action' value='removeFromCart'>"
							+ "<input type='hidden' name='voucher' value=" + voucher.getId() + ">"
							+ "<input type='submit' value='Remove From Cart'>");
					if (tvd.isAvailable(voucher.getId()) == false) {
						out.println("<font color='red'>NOT AVAILABLE</form>");
					}
					
				}
				
				}
				
				out.println("</h1>");
			} catch (IOException e) {
				LOG.error(e);
		}
		return SKIP_BODY;

	}

	public TouristVoucher getVoucher() {
		return voucher;
	}

	public void setVoucher(TouristVoucher voucher) {
		this.voucher = voucher;
	}

}
